/**
 * 数据备份路由
 */

import { Elysia, t } from 'elysia';
import {
  getBackups,
  createBackup,
  downloadBackup,
  deleteBackup,
  restoreBackup
} from '../controllers/backupController';
import { adminMiddleware } from '../middleware/auth';

export const backupRoutes = new Elysia({ prefix: '/backup' })
  // 所有备份接口都需要管理员权限
  .use(adminMiddleware())
  
  // 获取备份列表
  .get('/', getBackups, {
    detail: {
      tags: ['Backup'],
      summary: '获取备份列表',
      description: '获取数据备份列表，支持分页和筛选'
    },
    query: t.Object({
      page: t.Optional(t.String({ description: '页码，默认为1' })),
      limit: t.Optional(t.String({ description: '每页数量，默认为10' })),
      type: t.Optional(t.String({ description: '备份类型：manual, auto' })),
      status: t.Optional(t.String({ description: '备份状态：pending, processing, completed, failed' }))
    })
  })
  
  // 创建备份
  .post('/', createBackup, {
    detail: {
      tags: ['Backup'],
      summary: '创建数据备份',
      description: '创建新的数据备份，可选择要备份的表'
    },
    body: t.Object({
      name: t.String({ description: '备份名称' }),
      description: t.Optional(t.String({ description: '备份描述' })),
      tables: t.Optional(t.Array(t.String(), { description: '要备份的表名数组，为空则备份所有表' }))
    })
  })
  
  // 下载备份文件
  .get('/:id/download', downloadBackup, {
    detail: {
      tags: ['Backup'],
      summary: '下载备份文件',
      description: '下载指定备份的SQL文件'
    },
    params: t.Object({
      id: t.String({ description: '备份ID' })
    })
  })
  
  // 恢复备份
  .post('/:id/restore', restoreBackup, {
    detail: {
      tags: ['Backup'],
      summary: '恢复数据备份',
      description: '从指定备份恢复数据，此操作会覆盖当前数据'
    },
    params: t.Object({
      id: t.String({ description: '备份ID' })
    })
  })
  
  // 删除备份
  .delete('/:id', deleteBackup, {
    detail: {
      tags: ['Backup'],
      summary: '删除备份',
      description: '删除指定的备份记录和文件'
    },
    params: t.Object({
      id: t.String({ description: '备份ID' })
    })
  });